package macrochip.vison.com.gps.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.vison.baselibrary.utils.LogUtils;
import com.vison.baselibrary.widgets.HeliwayRockerView;
import com.vison.baselibrary.widgets.RockerGroupView;
import com.vison.baselibrary.widgets.RockerView;

/**
 * 重力摇杆
 * Created by XiaoShu
 * on 2017/8/8 11:33
 */

public class GravityUtils {
    private static SensorManager mSensorManager;
    private static Sensor mSensor;
    private HeliwayRockerView rockerView1, rockerView2;
    private static final float SPEED = 1.3f;

    public GravityUtils(Context context) {
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        if (null == mSensorManager) {
            LogUtils.e("deveice not support SensorManager");
        } else {
            // 重力传感器
            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
            if (null == mSensor) {
                // 如果没有重力就用方向加速传感器
                mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            }
        }
    }

    /**
     * 开启重力控制
     */
    public boolean startGravityControl(HeliwayRockerView rockerView1, HeliwayRockerView rockerView2) {
        this.rockerView1 = rockerView1;
        this.rockerView2 = rockerView2;
        return registerListener();
    }

    private boolean registerListener() {
        if (mSensorManager != null) {
            /* samplingPeriodUs：指定获取传感器频率，一共有如下几种：
            SensorManager.SENSOR_DELAY_FASTEST：最快，延迟最小，同时也最消耗资源，一般只有特别依赖传感器的应用使用该频率，否则不推荐。
            SensorManager.SENSOR_DELAY_GAME：适合游戏的频率，一般有实时性要求的应用适合使用这种频率。
            SensorManager.SENSOR_DELAY_NORMAL：正常频率，一般对实时性要求不高的应用适合使用这种频率。
            SensorManager.SENSOR_DELAY_UI：适合普通应用的频率，这种模式比较省电，而且系统开销小，但延迟大，因此只适合普通小程序使用。
            */
            mSensorManager.registerListener(sensorListener, mSensor,
                    SensorManager.SENSOR_DELAY_GAME);
            return true;
        }
        return false;
    }

    public void closeGravityControl() {
        rockerView1 = null;
        rockerView2 = null;

        if (mSensorManager != null) {
            mSensorManager.unregisterListener(sensorListener);
        }
    }

    private SensorEventListener sensorListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            float x = event.values[0];
            float y = event.values[1];
//                float z = event.values[2];

            // 修改取值范围0-9-18
            y += 9f;
            if (y < 0f) {
                y = 0f;
            } else if (y > 18f) {
                y = 18f;
            }

            x *= SPEED;
            x += 9f;
            if (x < 0f) {
                x = 0f;
            } else if (x > 18f) {
                x = 18f;
            }
            x = 18f - x;

            if (null != rockerView1) {
                int yvValue;

                float coefficient = rockerView1.getMaxX() / 18f;

                if (rockerView1.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    yvValue = (int) (x * coefficient);
                } else {
                    y = 18f - y;
                    yvValue = (int) (y * coefficient);
                }
                rockerView1.setYV(yvValue);
                rockerView1.refresh();
            }

            if (null != rockerView2) {
                int xvValue;

                float coefficient = rockerView2.getMaxX() / 18f;

                if (rockerView2.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    xvValue = (int) (y * coefficient);
                } else {
                    xvValue = (int) (x * coefficient);
                }

                rockerView2.setXV(xvValue);
                rockerView2.refresh();
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };
}
